// map.js
import qqmapsdk from '../../../libs/index'
Page({
  data: {
    latitude: 37.873084,
    longitude: 112.62939,
    marks: [
      {
        id: 1,
        latitude: 37.873084,
        longitude: 112.62939,
        width: 30,
        height: 35,
      },
      {
        id: 2,
        latitude: 37.871332,
        longitude: 112.63292,
        width: 20,
        height: 25,
        iconPath: '/static/images/marker.png',
      },
    ],
    polyline: [],
  },
  onLoad({ id }) {
    this.getDetail(id)
  },
  onShow() {
    qqmapsdk.direction({
      mode: 'bicycling',
      from: '37.871332,112.63292',
      to: '37.873084,112.62939',
      success: (res) => {
        console.log(res)
        let coors = res.result.routes[0].polyline,
          pl = []
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        let kr = 1000000
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [
            {
              points: pl,
              color: '#000',
              width: 2,
            },
          ],
        })
      },
    })
  },

  async getDetail(id) {
    const { data } = await wx.http.get(`/repair/${id}`)
    this.setData({ ...data })
  },
  // 修改传递参数
  editRepair(ev) {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + ev.mark.id,
    })
  },
  // 取消报修
  async cancel(ev) {
    await wx.http.put(`/cancel/repaire/${ev.mark.id}`)

    // 方案一: 停留在当前页面 修改当前报修信息的状态
    this.setData({ status: 0 })
    // 方案二: 返回报修列表页面
    // wx.navigateTo({
    //   url: '/repair_pkg/pages/list/index',
    // })
  },
})
